package com.xuecheng.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaFileProcessService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description TODO
 * @author Mr.M
 * @date 2022/9/14 14:41
 * @version 1.0
 */
@Slf4j
@Service
public class MediaFileProcessServiceImpl implements MediaFileProcessService {

 @Autowired
 MediaProcessMapper mediaProcessMapper;

 @Autowired
 MediaFilesMapper mediaFilesMapper ;

 @Autowired
 MediaProcessHistoryMapper mediaProcessHistoryMapper;



 @Override
 public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int count) {
  List<MediaProcess> mediaProcesses = mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
   return mediaProcesses;
 }

 public boolean startTask(long id) {
  int result = mediaProcessMapper.startTask(id);
  return result<=0?false:true;
 }


 @Override
 public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
  MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
  if(mediaProcess == null){
   return;
  }
//  如果任务执行失败
  if(status.equals("3")){
   mediaProcess.setFailCount(mediaProcess.getFailCount()+1);
   mediaProcess.setStatus("3");
   mediaProcess.setErrormsg(errorMsg);
  mediaProcessMapper.updateById(mediaProcess);
  }
//  如果任务执行成功
  MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
//  更新media_file表中的url
//
//  UpdateWrapper<MediaFiles> updateWrapper = new UpdateWrapper<>();
//  updateWrapper.set("url",url);
//  mediaFiles.setStatus(url);

  LambdaUpdateWrapper<MediaFiles>  lambdaUpdateWrapper= new LambdaUpdateWrapper<>();
  lambdaUpdateWrapper.set(MediaFiles::getUrl,url);
  mediaFilesMapper.update(mediaFiles,lambdaUpdateWrapper);

//  mediaProcess.setStatus("2");
//  mediaProcess.setFinishDate(LocalDateTime.now());
//  mediaProcess.setUrl(url);
  LambdaUpdateWrapper<MediaProcess>  updateWrapper2= new LambdaUpdateWrapper<>();
  updateWrapper2.set(MediaProcess::getStatus,"2");
  updateWrapper2.set(MediaProcess::getFinishDate,LocalDateTime.now());
  updateWrapper2.set(MediaProcess::getUrl,url);
  mediaProcessMapper.update(mediaProcess,updateWrapper2);

  MediaProcessHistory mediaProcessHistory = new MediaProcessHistory() ;
  BeanUtils.copyProperties(mediaProcess,mediaProcessHistory);
  mediaProcessHistoryMapper.insert(mediaProcessHistory) ;
  //MediaProcess删除任务
  mediaProcessMapper.deleteById(mediaProcess) ;
 }

}